package com.xmad.ksadutils.AdSDK.AdMold.Kwai;

import android.app.Activity;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsRewardVideoAd;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.SdkConfig;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.AdBase.AdCode;
import com.xmad.ksadutils.AdSDK.Interface.AdLoad;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KsLoadRewardVideoAd extends AdBaseLoad implements AdLoad {
    private volatile static KsLoadRewardVideoAd mInstance;
    KsRewardVideoAd mRewardVideoAd;
    int Position;
    /**
     * 单例模式
     *
     * @return
     */
    public static KsLoadRewardVideoAd getInstance() {
        if (mInstance == null) {
            synchronized (KsLoadRewardVideoAd.class) {
                if (mInstance == null) {
                    mInstance = new KsLoadRewardVideoAd();
                }
            }
        }
        return mInstance;
    }

    @Override
    public void Show(Activity activity, FrameLayout frameLayout) {
        Log.e(TAG,"激励视频开始加载");
        if (mRewardVideoAd == null){
            if (KsAdId==null&& AdCode.getKsLoadRewardVideoId()==0){
                Log.e(TAG,"KsLoadRewardVideoAd————————还未设置ID:");
                return;
            }
            if (ToastCode){
                Log.e(TAG,KsAdId+"mmmmmmmmmmm");
                Toast.makeText(activity,KsAdId!=null ? KsAdId+"": AdCode.getKsLoadRewardVideoId()+"",Toast.LENGTH_SHORT).show();
            }
            KsScene.Builder builder = new KsScene.Builder(KsAdId!=null ? KsAdId:AdCode.getKsLoadRewardVideoId()).screenOrientation(SdkConfig.SCREEN_ORIENTATION_UNKNOWN);
            Map<String, String> rewardCallbackExtraData = new HashMap<>();
            rewardCallbackExtraData.put("thirdUserId", "test-uerid-jia");
            rewardCallbackExtraData.put("extraData", "testExtraData");
            builder.rewardCallbackExtraData(rewardCallbackExtraData);
            KsScene scene = builder.build(); // 此为测试posId，请联系快手平台申请正式posId
            // 请求的期望屏幕方向传递为1，表示期望为竖屏
            KsAdSDK.getLoadManager().loadRewardVideoAd(scene, new KsLoadManager.RewardVideoAdListener() {
                @Override
                public void onError(int code, String msg) {
                   Log.e(TAG,"激励视频广告请求失败"+ code + msg+"-----------");
                        if (KsAds!=null&&KsAds.size()>0){
                            if (Position>=KsAds.size()){
                                Position=0;
                                return;
                            }
                            AdCode.setKsLoadRewardVideoId(KsAds.get(Position));
                            Log.e("激励视频重试请求————ID:",AdCode.getKsLoadRewardVideoId()+"--"+Position);
                            ++Position;
                            Show(activity,frameLayout);
                        }
                }

                @Override
                public void onRequestResult(int adNumber) {
                    Log.e(TAG,"激励视频广告请求结果返回"+ adNumber);
                }

                @Override
                public void onRewardVideoAdLoad(@Nullable List<KsRewardVideoAd> adList) {
                    Log.e(TAG,"激励视频预加载完成");
                    if (adList != null && adList.size() > 0) {
                        mRewardVideoAd = adList.get(0);
                        if (!Cache){
                            showRewardVideoAd(activity);
                        }else {
                            Log.e(TAG,"——————————下一个激励视频预加载完成————————");
                        }
                    }
                }
            });
        }else {
                showRewardVideoAd(activity);
        }

    }


    private void showRewardVideoAd(Activity activity) {
        if (mRewardVideoAd != null && mRewardVideoAd.isAdEnable()) {
            mRewardVideoAd.setRewardAdInteractionListener(new KsRewardVideoAd.RewardAdInteractionListener() {
                @Override
                public void onAdClicked() {
                    Log.e(TAG,"激励视频广告点击");
                }

                @Override
                public void onPageDismiss() {
                    Log.e(TAG,"激励视频广告关闭");
                    if (listener!=null){
                        listener.onPageDismiss();
                    }
                }

                @Override
                public void onVideoPlayError(int code, int extra) {
                    Log.e(TAG,"激励视频广告播放出错");
                }

                @Override
                public void onVideoPlayEnd() {
                    Log.e(TAG,"激励视频广告播放完成");
                }

                @Override
                public void onVideoSkipToEnd(long l) {

                }

                //                @Override
//                public void onVideoSkipToEnd(long l) {
//
//                }
                @Override
                public void onVideoPlayStart() {
                    Log.e(TAG,"激励视频广告播放开始");
                    if (listener!=null){
                        listener.onAdShow();

                    }
                    //Tool.tips();
                    setCache(true);
                    mRewardVideoAd=null;
                    Show(activity,null);
                }
                @Override
                public void onRewardVerify() {
                    Log.e(TAG,"激励视频广告获取激励");
                    if (listener!=null){
                        listener.onRewardVerify();
                    }

                }

            });

            mRewardVideoAd.showRewardVideoAd(activity, null);
         } else {
            Log.e(TAG,"暂无可用激励视频广告，请等待缓存加载或者重新刷新");
         }
    }







}
